package cn.tedu.tmall.basic.controller;

import cn.tedu.tmall.basic.pojo.vo.DistrictCacheVO;
import cn.tedu.tmall.basic.service.IDistrictService;
import cn.tedu.tmall.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/districts")
@Validated
@Api(tags = "1. 省市区数据管理")
public class DistrictController {

    @Autowired
    private IDistrictService districtService;

    @ApiOperation("根据父级查询子级行政单位列表")
    @ApiOperationSupport(order = 100)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父级行政单位ID", required = true, dataType = "long")
    })
    @GetMapping("/list-by-parent")
    public JsonResult listByParent(@RequestParam @Range(message = "查询子级行政单位列表失败，请提交有效的父级行政单位ID值！") Long parentId) {
        List<DistrictCacheVO> list = districtService.listByParent(parentId);
        return JsonResult.ok(list);
    }

    @ApiOperation("重建缓存")
    @ApiOperationSupport(order = 500)
    @PostMapping("/rebuild-cache")
    public JsonResult rebuildCache() {
        districtService.rebuildCache();
        return JsonResult.ok();
    }

}
